package com.zust.data.serivce;

import com.sun.org.apache.bcel.internal.generic.I2F;
import com.zust.data.dal.DlColumnRepository;
import com.zust.data.dal.DlDataRepository;
import com.zust.data.dal.UserRepository;
import com.zust.data.domain.param.ColumnSaveParam;
import com.zust.data.domain.vo.ColumnInfoVo;
import com.zust.data.dto.ResponseDto;
import com.zust.data.entity.DlColumn;
import com.zust.data.entity.DlData;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author senselyan
 * @Description: $
 * @date 2018/5/11 15:04
 */
@Service
public class ColumnService {

    @Inject
    private UserRepository userRepository;

    @Inject
    private DlDataRepository dlDataRepository;

    @Inject
    private DlColumnRepository dlColumnRepository;

    public ResponseDto save(ColumnSaveParam columnSaveParam) {
        if (StringUtils.isNotBlank(columnSaveParam.getId())) {
            DlColumn column = dlColumnRepository.findOne(columnSaveParam.getId());
            BeanUtils.copyProperties(columnSaveParam, column);
            return new ResponseDto(true, "操作成功", dlColumnRepository.save(column));
        }
        return new ResponseDto(false, "操作失败");
    }

    public ResponseDto update(String columnId, String field, String value) {
        DlColumn column = dlColumnRepository.findOne(columnId);
        switch (field) {
            case "name": column.setName(value);break;
            case "valueA": column.setValueA(value);break;
            case "valueB": column.setValueB(value);break;
            case "valueC": column.setValueC(value);break;
            case "valueD": column.setValueD(value);break;
            case "valueE": column.setValueE(value);break;
        }
        dlColumnRepository.save(column);
        return new ResponseDto(true, "操作成功");
    }

    public ResponseDto addEmptys(String dataId, Integer num) {
        DlData dlData = dlDataRepository.findOne(dataId);
        String userId = dlData.getUserId();
        List<DlColumn> dlColumns = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            dlColumns.add(addEmpty(userId, dataId));
        }
        return new ResponseDto(true, "操作成功", dlColumns);
    }

    public DlColumn addEmpty(String userId, String dataId) {
        return dlColumnRepository.save(new DlColumn(userId, dataId));
    }

    public List<ColumnInfoVo> listByDataId(String dataId) {
        List<DlColumn> dlColumns = dlColumnRepository.findAllByDataIdAndDeletedFlag(dataId, 0);
        return dlColumns.stream().map((DlColumn dlColumn) -> {
            ColumnInfoVo columnInfoVo = new ColumnInfoVo();
            BeanUtils.copyProperties(dlColumn, columnInfoVo);
            return columnInfoVo;
        }).collect(Collectors.toList());
    }


    public void delete(String dataId) {
        dlColumnRepository.deleteAllByDataId(dataId);
    }

    public void deleted(String dataId) {
        DlColumn column = dlColumnRepository.findOne(dataId);
        column.setDeletedFlag(1);
        dlColumnRepository.save(column);
    }


}
